
/**
 * 动态事件绑定方法
 * 为构造函数添加动态事件绑定和执行方法
 * @export
 * @class Events
 * @example
 * // 使用
 * class _Event extends Events { ... }
 */
export default class Events {
    /**
    * 绑定方法缓存对象
    * @memberof Events
    */
    _$event = {}
    constructor() { }
    /**
     * 对象绑定方法
     *
     * @param {String} str 绑定名称
     * @param {Function} event 需要绑定方法
     * @example 
     *  _Event.on('string',function(str,arg1,arg2){ ... });
     * @memberof Events
     */
    on(str, event, scope) {
        if (!this._$event.hasOwnProperty(str)) {
            this._$event[str] = [];
        }
        this._$event[str].push({ event, scope });
    }
    /**
     * 一次性对象绑定方法
     *
     * @param {String} str 绑定名称
     * @param {Function} event 需要绑定方法
     * @example 
     *  _Event.on('string',function(str,arg1,arg2){ ... });
     * @memberof Events
     */
    once(str, event, scope) {
        if (!this._$event.hasOwnProperty(str)) {
            this._$event[str] = [];
        }
        this._$event[str].push({ event, scope, once: true });
    }
    /**
     * 取消事件绑定方法
     * 
     * @param {String} str 绑定名称
     * @param {Function=} event 需要取消绑定的方法
     * @example
     * var fun = function(arg1,arg2){ ... };
     * // 单独取消
     * _Event.off('string',fun);
     * // 全部取消
     * _Event.off('string');
     * @memberof Events
     */
    off(str, event) {
        if (str) {
            if (event) {
                let events = this._$event.hasOwnProperty(str) ? this._$event[str] : [];
                for (let i = 0; i < events.length; i++) {
                    const element = events[i];
                    if (element.event === event) {
                        events.splice(i, 1);
                        break;
                    }
                }
            } else {
                delete this._$event[str];
            }
        } else {
            for (const key in this._$event) {
                const events = this._$event[key];
                for (let i = 0; i < events.length; i++) {
                    events.event = null;
                    events.scope = null;
                    delete events[i];
                }
                delete this._$event[key];
            }
            this._$event = null;
        }
    }
    /**
     * 绑定事件运行方法
     * 
     * @param {String} str 绑定的事件名称 
     * @param {Object} that 绑定事件的作用域 
     * @param {*} [object] 任意需要传值的参数
     * @example
     *  _Event.run(str, this, 'string', {}, [], null ...);
     * @memberof Events
     */
    run() {
        const result = [...arguments];
        const str = result.shift();
        const events = this._$event.hasOwnProperty(str) ? this._$event[str] : [];
        const onceArray = [];
        for (let i = 0; i < events.length; i++) {
            const element = events[i];
            let goon = element.event.apply(element.scope || this, result);
            if (element.once) {
                onceArray.push(element);
            }
            if (goon === false) {
                break;
            }
        };

        for (let i = 0; i < onceArray.length; i++) {
            const element = onceArray[i];
            this.off(str, element.event);
        };

    }
}